package com.test;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.BookDao;
import com.itheima.domain.Book;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.*;

public class Demo1 {
    @Test
    public void test1() throws IOException {
        //将配置文件装换成流文件
        InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");
        //读取流文件，创建session对象 此对象用于执行sql语句和事务管理
        SqlSessionFactoryBuilder factory=new SqlSessionFactoryBuilder();
        SqlSessionFactory sessionFactory = factory.build(in);
        //自动提交 sessionFactory。openSession（true）
        SqlSession session =sessionFactory.openSession(true);
        //创建代理对象
        BookDao mapper=session.getMapper(BookDao.class);

        List<Book>list=mapper.findByCase("","");
        for (Book book : list) {
            System.out.println(book);
        }


//        模糊查寻出版社中带有'人民'且书籍单价大于100小于200的书籍
//        Map<String,Object> map=new HashMap<>();
//        map.put("company","人民");
//        map.put("param1",100);
//        map.put("param2",200);
//        List<Book> list=mapper.find(map);
//        for (Book book1 : list) {
//            System.out.println(book1);
//        }
//


        //查询id为1，3，5的书籍(如果集合为空则查询全部数据)
//        List<Integer> list=new ArrayList<Integer>();
//        list.add(1);
//        list.add(3);
//        list.add(5);
//        List<Book> bookList = mapper.findByList(list);
//        for (Book book : bookList) {
//            System.out.println(book);
//        }




//将id为20的书籍，单价修改为500元 并返回影响行数
//        int update = mapper.update(20, 500.00);
//        System.out.println(update);


//        int[]ids={21};
//        int i=mapper.del(ids);
//        System.out.println(i);


//        Book book=new Book();
//        book.setCompany("人民");
//        book.setPrice(100d);
//        List<Book> list=mapper.findByCase(book);
//        for (Book book1 : list) {
//            System.out.println(book1);
//        }


//        PageHelper.startPage(3,5);
//        List<Book> list=mapper.showAll();
//        for (Book book : list) {
//            System.out.println(book);
//        }
//        PageInfo<Book> pageInfo=new PageInfo<>(list);
//        System.out.println("上一页"+pageInfo.getPrePage());
//        System.out.println("下一页"+pageInfo.getNextPage());
//        System.out.println("总页数"+pageInfo.getPages());
//        Book book = new Book();
//        book.setBookName("aaa");
//        book.setAuthor("bb");
//        book.setKid(1);
//        book.setPrice(100d);
//        book.setCompany("abc");
//        book.setBirthday(new Date());
//        mapper.save(book);
//        List<Book>list=mapper.findOne4();
//        for (Book book : list) {
//            System.out.println(book);
//        }

//        Map<String,Object> map=new HashMap<>();
//        map.put("a",7);
//        map.put("b","国际金融");
//        Book one3=mapper.findOne3(map);
//        System.out.println(one3);
    }
}
